package org.example.account.service.impl;

import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.BaseEntity;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import org.apache.commons.lang3.StringUtils;
import org.example.utils.Util;
import org.example.warehouse.domain.MallStoreDetail;
import org.example.warehouse.domain.SellStoreDetail;
import org.example.warehouse.mapper.MallStoreDetailMapper;
import org.example.warehouse.mapper.SellStoreDetailMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.example.account.mapper.AccountMapper;
import org.example.account.domain.Account;
import org.example.account.service.IAccountService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 对账单Service业务层处理
 *
 * @author 小孟微信：xiaomengcode
 * @date 2024-04-20
 */
@Service
public class AccountServiceImpl implements IAccountService
{
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private MallStoreDetailMapper mallStoreDetailMapper;

    @Autowired
    private SellStoreDetailMapper sellStoreDetailMapper;
    /**
     * 查询对账单
     *
     * @param accountId 对账单主键
     * @return 对账单
     */
    @Override
    public Account selectAccountByAccountId(Long accountId)
    {
        return accountMapper.selectAccountByAccountId(accountId);
    }

    /**
     * 查询对账单列表
     *
     * @param account 对账单
     * @return 对账单
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<Account> selectAccountList(Account account)
    {
        Util.setQuerySelfData((BaseEntity) account);
        return accountMapper.selectAccountList(account);
    }

    /**
     * 新增对账单
     *
     * @param account 对账单
     * @return 结果
     */
    @Override
    @Transactional
    public int insertAccount(Account account)
    {
        account.setCreateBy(ShiroUtils.getUserId()+"");
        account.setCreateTime(DateUtils.getNowDate());
        account.setRemainMoney(account.getTotalMoney());
        int i = accountMapper.insertAccount(account);

        String searchValue = account.getSearchValue();
        List<Long> ids= JSONArray.parseArray(searchValue, Long.class);

        if(0==account.getType()){
            MallStoreDetail mallStoreDetail = new MallStoreDetail();
            mallStoreDetail.getParams().put("ids",ids);
            mallStoreDetail.setAccountId(account.getAccountId());
            mallStoreDetailMapper.updateMallStoreDetailAccountId(mallStoreDetail);
        }else{
            SellStoreDetail sellStoreDetail = new SellStoreDetail();
            sellStoreDetail.getParams().put("ids",ids);
            sellStoreDetail.setAccountId(account.getAccountId());
           sellStoreDetailMapper.updateSellStoreDetailAccountId(sellStoreDetail);
        }
        return i;
    }

    /**
     * 修改对账单
     *
     * @param account 对账单
     * @return 结果
     */
    @Override
    public int updateAccount(Account account)
    {
        return accountMapper.updateAccount(account);
    }

    /**
     * 批量删除对账单
     *
     * @param accountIds 需要删除的对账单主键
     * @return 结果
     */
    @Override
    public int deleteAccountByAccountIds(String accountIds)
    {
        return accountMapper.deleteAccountByAccountIds(Convert.toStrArray(accountIds));
    }

    /**
     * 删除对账单信息
     *
     * @param accountId 对账单主键
     * @return 结果
     */
    @Override
    public int deleteAccountByAccountId(Long accountId)
    {
        return accountMapper.deleteAccountByAccountId(accountId);
    }
}
